IRCBot::Plugin.define "lyrics" do
	help_string	"Looks up lyrics. Usage: lyrics \"Band - Song\""
	def command(user,target,message,usermask)
		params = message
		if !params.empty? then
			if params.split(" - ").count != 2 then
				privmsg(target,"You're doing it wrong. RTFM, please.")		
			else
				band = params.split(" - ")[0]
				song = params.split(" - ")[1]
				returning = get_lyrics(band,song)
				if returning.class == NilClass then
					privmsg(target,"Not found. Sorry.")
				else
					for i in returning
						privmsg(target,i)
					end
				end
			end
		else
			privmsg(target,"You're doing it wrong, RTFM")
		end
	end
	
	def get_lyrics(band,song)
		band=band.downcase.strip.gsub(" ","-")
		song=song.downcase.strip.gsub(" ","-").gsub(/(\,|\!|\?|\"|\.|\')/,"")
		puts band
		puts song
		Net::HTTP.start("vagalume.uol.com.br",80) do |http|
			req = Net::HTTP::Get.new("/#{band}/#{song}.html")
			$request = http.request(req)
		end
		if $request.message == "Not Found" then
			return ["Not found"]
		elsif $request.message == "OK" then
			doc = Hpricot($request.body)
			lyrics = doc.search("div.tab_original").to_s
			lyrics = lyrics.gsub("<br />","").gsub(/<(.+?)>/,"")
			newstanzas = []
			stanzas = lyrics.split("\n\n")
			for stanza in stanzas
				newstanzas.push stanza.split("\n").join(" / ")
			end
			return newstanzas
		end
	end
end
